﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DatabaseService.DatabaseDAO
{
    public class MapperDictionary : IDictionary<string, object>
    {
        private readonly object _Lock = new object();
        private Dictionary<string, object> _dic = new Dictionary<string, object>();

        public object this[string key] { get => _dic[key]; set => _dic[key] = value; }

        public ICollection<string> Keys => _dic.Keys;

        public ICollection<object> Values => _dic.Values;

        public int Count => _dic.Count;

        public bool IsReadOnly => false;

        /// <summary>
        /// 值类型struct不支持传null
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Add(string key, object value)
        {
            if (string.IsNullOrWhiteSpace(key))
                return;
            object cvalue = value;
            if (value == null)
            {
                //if (value is String)
                //    cvalue = "";
                //else if(value is int?)
                //    cvalue = Common.getInt("");
                //else if (value is float?)
                //    cvalue = Common.getFloat("");
                //else if (value is double?)
                //    cvalue = Common.getDouble("");
                //else if (value is DateTime?)
                //    cvalue = Common.getDateTimeStr("");
                cvalue = "";
            }
            if (!key.StartsWith("@"))
                key = "@" + key;
            lock (_Lock)
            {
                if (!this._dic.ContainsKey(key))
                    this._dic.Add(key, cvalue);
            }
        }

        public void Add(KeyValuePair<string, object> item)
        {
            this.Add(item.Key, item.Value);
        }

        public void Clear()
        {
            this._dic.Clear();
        }

        public bool Contains(KeyValuePair<string, object> item)
        {
            return this._dic.Contains(item);
        }

        public bool ContainsKey(string key)
        {
            return this._dic.ContainsKey(key);
        }

        public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
        {
            if (array == null) throw new ArgumentNullException("array");
            int num = Count;
            lock(_Lock)
            {
                foreach (KeyValuePair<string, object> kvp in this._dic)
                {
                    array[arrayIndex++] = kvp;
                    if (--num <= 0)
                        return;
                }
            }
        }

        public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
        {
            lock (_Lock)
            {
                foreach (var item in this._dic)
                {
                    yield return item;
                }
            }
        }

        public bool Remove(string key)
        {
            lock (_Lock)
            {
                return this._dic.Remove(key);
            }
        }

        public bool Remove(KeyValuePair<string, object> item)
        {
            return Remove(item.Key);
        }

        public bool TryGetValue(string key, out object value)
        {
            return this._dic.TryGetValue(key, out value);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this._dic.GetEnumerator();
        }
    }
}
